﻿

(function ($) {
    $.fn.extend({
        searchBar: function (options) {
            if (options && typeof (options) == 'object') {
                options = $.extend({}, $.searchBar.defaults, options);
            } else {
                options = $.searchBar.defaults;
            }

            this.each(function () {
                new $.searchBar(this, options);
            });
            return $(this);
        }
    });

    $.searchBar = function (elem, options) {
        fixParametterForSearchForm();
        fixWidthForSearchText();

        //$("#selectCategoryButton,#selectcategorylist").hover(
        //    function () {
        //        $("#selectcategorylist").show();
        //    },
        //    function () {
        //        $("#selectcategorylist").hide();
        //    }
        //);
        $(elem).find('.icon_search_w').click(function () {
            $(elem).submit();
        });

        $("#selectcategorylist").find("li,p,a").click(function () {
            var $spam = $("#selectCategoryButton>span");
            if ($(this).text() == "Tất cả ngành hàng") {
                $spam.text("Tất cả");
                options.cateURL = "";
            }
            else {
                $spam.text($(this).text());
                if (this.tagName == "A") {
                    options.cateURL = $(this).attr("href");

                }
                else
                    options.cateURL = $(this).find("a").attr("href");
            }
            $("#selectcategorylist").hide();
            fixWidthForSearchText();
            return false;
        });

        $(elem).submit(function () {

            var queryString = getParametterFormTextbox($(this).find("#search_input"));
            
            if (queryString != '') {
                
                if (options.cateURL != "")
                    var url = window.location.protocol + "//" + window.location.host + options.cateURL + '?q=' + encodeURIComponent(queryString);
                else
                    var url = window.location.protocol + "//" + window.location.host + options.cityAlias + '/tim-kiem/' + encodeURIComponent(queryString) + ".html";

                window.location = url;
            }
            return false;
        });
        
        

        function fixParametterForSearchForm() {
            var $input = $(elem).find("#search_input");
            if ($input.length > 0) {
                var queryString = $input.val();
                queryString = queryString.replace(/-/g, ' ');
                $input.val(queryString);
            }
        }

        function fixWidthForSearchText() {
            var $spam = $("#selectCategoryButton>span");
            var spamWidth = $spam.outerWidth();
            var totalWidth = $(elem).find(".bg_search").width();
            //var $input = $(elem).find("#search_input").width(totalWidth - spamWidth - 55);
        }
        $.ajax({
            type: "POST",
            url: "/index.php/ajax/searchSalePoint",
            data: {},
            success: function (data) {
                $(elem).find("#search_input").autocomplete(data, { width: 238, max: 5, noRecord: "" });
            }
        });
    };

    $.searchBar.defaults = {
        cateURL: ""
    };

})(jQuery);

